{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3a11aa5c-e69e-4bfa-b4b1-4b053466cb9c",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torchvision.datasets as ds\n",
    "import torchvision.transforms as ts\n",
    "from torch.utils import data\n",
    "from torch.autograd import Variable\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "557e26d1-f9f8-419b-9621-f2462e29fe07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<torch._C.Generator at 0x25e0ebefcb0>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.manual_seed(777)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5180a69f",
   "metadata": {},
   "outputs": [],
   "source": [
    "#参数设置\n",
    "batch_size=100\n",
    "learning_rate=0.001\n",
    "train_epochs=15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "04e93f06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz\n",
      "Failed to download (trying next):\n",
      "HTTP Error 403: Forbidden\n",
      "\n",
      "Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-images-idx3-ubyte.gz\n",
      "Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-images-idx3-ubyte.gz to DATA/MNIST_data\\MNIST\\raw\\train-images-idx3-ubyte.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100.0%\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting DATA/MNIST_data\\MNIST\\raw\\train-images-idx3-ubyte.gz to DATA/MNIST_data\\MNIST\\raw\n",
      "\n",
      "Downloading http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz\n",
      "Failed to download (trying next):\n",
      "HTTP Error 403: Forbidden\n",
      "\n",
      "Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-labels-idx1-ubyte.gz\n",
      "Downloading https://ossci-datasets.s3.amazonaws.com/mnist/train-labels-idx1-ubyte.gz to DATA/MNIST_data\\MNIST\\raw\\train-labels-idx1-ubyte.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100.0%\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting DATA/MNIST_data\\MNIST\\raw\\train-labels-idx1-ubyte.gz to DATA/MNIST_data\\MNIST\\raw\n",
      "\n",
      "Downloading http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz\n",
      "Failed to download (trying next):\n",
      "HTTP Error 403: Forbidden\n",
      "\n",
      "Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-images-idx3-ubyte.gz\n",
      "Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-images-idx3-ubyte.gz to DATA/MNIST_data\\MNIST\\raw\\t10k-images-idx3-ubyte.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100.0%\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting DATA/MNIST_data\\MNIST\\raw\\t10k-images-idx3-ubyte.gz to DATA/MNIST_data\\MNIST\\raw\n",
      "\n",
      "Downloading http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz\n",
      "Failed to download (trying next):\n",
      "HTTP Error 403: Forbidden\n",
      "\n",
      "Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-labels-idx1-ubyte.gz\n",
      "Downloading https://ossci-datasets.s3.amazonaws.com/mnist/t10k-labels-idx1-ubyte.gz to DATA/MNIST_data\\MNIST\\raw\\t10k-labels-idx1-ubyte.gz\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100.0%"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting DATA/MNIST_data\\MNIST\\raw\\t10k-labels-idx1-ubyte.gz to DATA/MNIST_data\\MNIST\\raw\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "#下载训练集\n",
    "ds_train=ds.MNIST(root=r'DATA/MNIST_data',\n",
    "                                 train=True,\n",
    "                                 transform=ts.ToTensor(),\n",
    "                                 download=True)#60000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4f27f157",
   "metadata": {},
   "outputs": [],
   "source": [
    "#下载测试集\n",
    "ds_test=ds.MNIST(root=r'DATA/MNIST_data',\n",
    "                                train=False,\n",
    "                                transform=ts.ToTensor(),\n",
    "                                download=True)#10000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "88c1cc44",
   "metadata": {},
   "outputs": [],
   "source": [
    "#数据迭代器\n",
    "dl=data.DataLoader(ds_train,batch_size=batch_size,shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6e55217d",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "#创建模型\n",
    "model=torch.nn.Linear(784,10,bias=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dbc817a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "#定义代价函数和优化器\n",
    "criterion=torch.nn.CrossEntropyLoss()#内部计算包含Softmax\n",
    "optimizer=torch.optim.Adam(model.parameters(),lr=learning_rate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "02110192",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 0.6143227219581604\n",
      "2 0.3445148766040802\n",
      "Learning Finish!\n"
     ]
    }
   ],
   "source": [
    "#训练模型\n",
    "for epoch in range(2):\n",
    "    avg_cost=0\n",
    "    total_batch=len(ds_train)//batch_size\n",
    "\n",
    "    for i,(x,y) in enumerate(dl):\n",
    "        x=Variable(x.view(-1,28*28))#输入图片格式改为[100,784]---符合模型的输入特征784\n",
    "        y=Variable(y)#[100]---标签不是独热编码\n",
    "\n",
    "        optimizer.zero_grad()\n",
    "        h=model(x)\n",
    "        cost=criterion(h,y)\n",
    "        cost.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "        avg_cost+=cost/total_batch\n",
    "    print(epoch+1,avg_cost.item())\n",
    "\n",
    "print('Learning Finish!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f4ffd614",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\Users\\28379\\anaconda3\\envs\\PY311\\Lib\\site-packages\\torchvision\\datasets\\mnist.py:81: UserWarning: test_data has been renamed data\n",
      "  warnings.warn(\"test_data has been renamed data\")\n",
      "d:\\Users\\28379\\anaconda3\\envs\\PY311\\Lib\\site-packages\\torchvision\\datasets\\mnist.py:71: UserWarning: test_labels has been renamed targets\n",
      "  warnings.warn(\"test_labels has been renamed targets\")\n"
     ]
    }
   ],
   "source": [
    "#测试模型,计算准确率\n",
    "x_test=Variable(ds_test.test_data.view(-1,28*28).float())#[10000, 784]\n",
    "y_test=Variable(ds_test.test_labels)#[10000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "77434aeb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "accuracy: tensor(0.9131)\n"
     ]
    }
   ],
   "source": [
    "prediction=model(x_test)\n",
    "prediction=torch.max(prediction,1)[1].float()\n",
    "accuracy=(prediction==y_test.float()).float().mean()\n",
    "print('accuracy:',accuracy)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cd00e05c",
   "metadata": {},
   "outputs": [],
   "source": [
    "#在测试集中任取一个样本测试模型\n",
    "r=random.randint(0,len(ds_test)-1)\n",
    "x_r=Variable(ds_test.test_data[r:r+1].view(-1,28*28).float())\n",
    "y_r=Variable(ds_test.test_labels[r:r+1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7d2a3d31",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([8])\n",
      "tensor([8.])\n",
      "tensor(1.)\n"
     ]
    }
   ],
   "source": [
    "pre_r=model(x_r)#[1,10]\n",
    "pre_r=torch.max(pre_r,1)[1].float()\n",
    "accuracy=(pre_r==y_r.data.float()).float().mean()\n",
    "print(y_r)\n",
    "print(pre_r)\n",
    "print(accuracy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "ee2cf48f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torchvision.datasets as ds\n",
    "import torchvision.transforms as ts\n",
    "from torch.utils.data import DataLoader\n",
    "from torch.autograd import Variable\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0d3d973a",
   "metadata": {},
   "outputs": [],
   "source": [
    "batch_size=100\n",
    "epochs=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "cd784d2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds_train=ds.MNIST(root=r'DATA/MNIST_data',\n",
    "                  train=True,\n",
    "                  transform=ts.ToTensor(),\n",
    "                  download=True)\n",
    "ds_test=ds.MNIST(root=r'DATA/MNIST_data',\n",
    "                  train=False,\n",
    "                  transform=ts.ToTensor(),\n",
    "                  download=True)\n",
    "dl=DataLoader(ds_train,batch_size=batch_size,shuffle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7b9b64e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#创建深层网络模型\n",
    "linear1=torch.nn.Linear(784,512)\n",
    "linear2=torch.nn.Linear(512,512)\n",
    "linear3=torch.nn.Linear(512,512)\n",
    "linear4=torch.nn.Linear(512,512)\n",
    "linear5=torch.nn.Linear(512,10)\n",
    "relu=torch.nn.ReLU()\n",
    "dropout=torch.nn.Dropout(0.3)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "61f82807",
   "metadata": {},
   "outputs": [],
   "source": [
    "# keep_prob=0.7#保持激活状态的概率\n",
    "# dropout=torch.nn.Dropout(p=1-keep_prob)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "883a6fa5",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\28379\\AppData\\Local\\Temp\\ipykernel_105684\\814290992.py:2: FutureWarning: `nn.init.xavier_uniform` is now deprecated in favor of `nn.init.xavier_uniform_`.\n",
      "  torch.nn.init.xavier_uniform(linear1.weight)\n",
      "C:\\Users\\28379\\AppData\\Local\\Temp\\ipykernel_105684\\814290992.py:3: FutureWarning: `nn.init.xavier_uniform` is now deprecated in favor of `nn.init.xavier_uniform_`.\n",
      "  torch.nn.init.xavier_uniform(linear2.weight)\n",
      "C:\\Users\\28379\\AppData\\Local\\Temp\\ipykernel_105684\\814290992.py:4: FutureWarning: `nn.init.xavier_uniform` is now deprecated in favor of `nn.init.xavier_uniform_`.\n",
      "  torch.nn.init.xavier_uniform(linear3.weight)\n",
      "C:\\Users\\28379\\AppData\\Local\\Temp\\ipykernel_105684\\814290992.py:5: FutureWarning: `nn.init.xavier_uniform` is now deprecated in favor of `nn.init.xavier_uniform_`.\n",
      "  torch.nn.init.xavier_uniform(linear4.weight)\n",
      "C:\\Users\\28379\\AppData\\Local\\Temp\\ipykernel_105684\\814290992.py:6: FutureWarning: `nn.init.xavier_uniform` is now deprecated in favor of `nn.init.xavier_uniform_`.\n",
      "  torch.nn.init.xavier_uniform(linear5.weight)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Parameter containing:\n",
       "tensor([[ 0.0306, -0.0685,  0.0704,  ...,  0.0841,  0.0044, -0.0111],\n",
       "        [-0.0518,  0.0552, -0.0508,  ..., -0.0623, -0.0087,  0.0861],\n",
       "        [-0.0638, -0.0745, -0.0401,  ...,  0.0457,  0.0662,  0.0220],\n",
       "        ...,\n",
       "        [-0.0849,  0.0724,  0.0605,  ...,  0.0269, -0.0864,  0.0835],\n",
       "        [-0.0331, -0.0684,  0.0417,  ..., -0.0375, -0.0741,  0.0778],\n",
       "        [-0.0395,  0.0687, -0.0634,  ...,  0.0887, -0.1004,  0.0694]],\n",
       "       requires_grad=True)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#xavier神经网络初始化方法----权重初始化\n",
    "torch.nn.init.xavier_uniform(linear1.weight)\n",
    "torch.nn.init.xavier_uniform(linear2.weight)\n",
    "torch.nn.init.xavier_uniform(linear3.weight)\n",
    "torch.nn.init.xavier_uniform(linear4.weight)\n",
    "torch.nn.init.xavier_uniform(linear5.weight)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "93a5c2cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "model=torch.nn.Sequential(linear1,relu,dropout,\n",
    "                          linear2,relu,dropout,\n",
    "                          linear3,relu,dropout,\n",
    "                          linear4,relu,dropout,\n",
    "                          linear5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "31ef824e",
   "metadata": {},
   "outputs": [],
   "source": [
    "criterion=torch.nn.CrossEntropyLoss()\n",
    "optimizer=torch.optim.Adam(model.parameters(),lr=0.001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ffc9e067",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3 0.3080989122390747\n",
      "3 0.14460748434066772\n"
     ]
    }
   ],
   "source": [
    "for epoch in range(epochs):\n",
    "    avg_cost=0\n",
    "    total_batch=len(ds_train)//batch_size\n",
    "    for i,(batch_xs,batch_ys) in enumerate(dl):\n",
    "        x=Variable(batch_xs.view(-1,784))\n",
    "        y=Variable(batch_ys)\n",
    "\n",
    "        optimizer.zero_grad()\n",
    "        h=model(x)\n",
    "        cost=criterion(h,y)\n",
    "        cost.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "        avg_cost+=cost/total_batch\n",
    "    print(epochs+1,avg_cost.item())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "d93b109b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9553\n"
     ]
    }
   ],
   "source": [
    "x_test=Variable(ds_test.test_data.view(-1,784).float())\n",
    "y_test=Variable(ds_test.test_labels)\n",
    "pre=model(x_test)\n",
    "pre=torch.max(pre.data,1)[1]\n",
    "acc=(pre==y_test.data).float().mean()\n",
    "print(acc.data.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "eceeacd9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n"
     ]
    }
   ],
   "source": [
    "r=random.randint(0,len(ds_test)-1)\n",
    "x_r=Variable(ds_test.test_data[r:r+1].view(-1,784).float())\n",
    "y_r=Variable(ds_test.test_labels[r:r+1])\n",
    "pre_r=model(x_r)\n",
    "pre_r=torch.max(pre_r.data,1)[1]\n",
    "acc_r=(pre_r==y_r.data).float().mean()\n",
    "print(acc_r.data.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "30aaf34d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
